/*
 *  Copyright (C) 2011 ryansullivan
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package client;

import java.awt.Point;
import java.awt.Robot;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

/**
 *
 * @author ryansullivan
 */
public class mouse implements MouseListener, MouseMotionListener  {

    CommandClient client;
    Point centerPoint;
    InputWindow window;
    Robot rob;
    boolean reset;

    public mouse(Point center, InputWindow parent) throws Exception{
        client = new CommandClient("10.14.183.25");
        centerPoint = center;
        window = parent;
        rob = new Robot();
        reset = false;
    }

    public void mouseMoved(MouseEvent me){
        if(!reset){
            System.out.println("Moved");
            Point difference = calculateMovement(me.getPoint());
            try{
                client.sendToServer("1:"+difference.x+","+difference.y);
            }catch(Exception e){}
            reset = true;
            rob.mouseMove(centerPoint.y, centerPoint.x);
            reset = false;
        }
    }

    public void mouseDragged(MouseEvent me){
        System.out.println("Dragged");
    }

    public void mouseClicked(MouseEvent me) {
        
    }

    public void mouseEntered(MouseEvent me) {
        System.out.println("Mouse Entered");
    }

    public void mouseExited(MouseEvent me) {
        System.out.println("Mouse Exited");
    }

    public void mousePressed(MouseEvent me) {
        window.setVisible(false);
        //System.out.println("Pressed");
    }

    public void mouseReleased(MouseEvent me) {
        System.out.println("Released");
    }

    private Point calculateMovement(Point movedTo){
        int x = centerPoint.x - movedTo.x;
        int y = centerPoint.y - movedTo.y;
        return new Point(x, y);
    }
}
